$(function() {

    var receiver = {
        getList: function() {
            $.ajax({
                url: wsCommon.host + '/api/memberAddrs/queryByMemberId',
                dataType: wsCommon.jsonType,
                success: function(res) {
                    if(res.code == 1) {
                        if(!res.result) {
                            wsCommon.setCookie('go_back_link', window.location.href);
                            window.location.href = './passport-login.html';
                            return;
                        }
                        $('[data-role="resultnum"]').text(res.data.memberAddrsList.length);
                        var tpl = '';
                        $.each(res.data.memberAddrsList, function(index, item) {
                            var defaultTpl = '';
                            var highlight = '';
                            if(item.defAddr == 1) {
                                defaultTpl = '<span class="label-default">默认地址</span>';
                                highlight = 'highlight';
                            } else {
                                defaultTpl = '<a href="javascript:;" class="action-set-default" data-addrId="'+item.addrId+'">设为默认</a>';
                            }
                            tpl += '<tr class="'+highlight+'">\
                                        <td class="shipping-address">'+item.area + item.addr+'</td>\
                                        <td class="shipping-name">'+item.name+'</td>\
                                        <td class="shipping-contact">'+item.mobile+'</td>\
                                        <td class="actions">\
                                          <span class="edit-text">正在编辑...</span>\
                                          <span class="edit-act">'+defaultTpl+'\
                                            <a href="javascript:;" class="action-edit" data-addrId="'+item.addrId+'" data-role="addredit" data-region="'+item.regionId+'" data-name="'+item.name+'" data-addr="'+item.addr+'" data-zip="'+item.zip+'" data-tel="'+item.tel+'" data-mobile="'+item.mobile+'">编辑</a>\
                                            <a href="javascript:;" class="" data-role="addrdel" data-addrId="'+item.addrId+'">删除</a>\
                                          </span>\
                                        </td>\
                                      </tr>';
                        })
                        $('.member-address-list tbody').html(tpl);

                        // 设置地区
                        var diquTpl = '<option value="-1">请选择</option>';
                        $.each(res.data.regionsList, function(index, item) {
                            diquTpl += '<option value="'+item.regionId+'">'+item.localName+'</option>';
                        })
                        $('[data-role="region"]').html(diquTpl);
                    } else {
                        iosOverlay({
                            text: res.reason,
                            duration: 1000
                        });
                    }
                },
                error: function(e) {
                }
            })
        },
        setDefault: function() {
            $(document).on('click', '.action-set-default', function() {
                var addrId = $(this).attr('data-addrId');
                $.ajax({
                    url: wsCommon.host + '/api/memberAddrs/setDefAddr',
                    dataType: wsCommon.jsonType,
                    data: {
                        addrId: addrId
                    },
                    success: function(res) {
                        if(res.code == 1) {
                            if(!res.result) {
                                wsCommon.setCookie('go_back_link', window.location.href);
                                window.location.href = './passport-login.html';
                                return;
                            }
                            iosOverlay({
                                text: '设置默认地址成功',
                                duration: 1000,
                                onbeforehide: function() {
                                    window.location.reload()
                                }
                            });
                        } else {
                            iosOverlay({
                                text: res.reason,
                                duration: 1000
                            });
                        }
                    },
                    error: function(e) {
                    }
                })
            })
        },
        delAddr: function() {
            $(document).on('click', '[data-role="addrdel"]', function() {
                var addrId = $(this).attr('data-addrId');
                $.ajax({
                    url: wsCommon.host + '/api/memberAddrs/delete',
                    dataType: wsCommon.jsonType,
                    data: {
                        addrId: addrId
                    },
                    success: function(res) {
                        if(res.code == 1) {
                            if(!res.result) {
                                wsCommon.setCookie('go_back_link', window.location.href);
                                window.location.href = './passport-login.html';
                                return;
                            }
                            iosOverlay({
                                text: '删除成功',
                                duration: 1000,
                                onbeforehide: function() {
                                    window.location.reload()
                                }
                            });
                        } else {
                            iosOverlay({
                                text: res.reason,
                                duration: 1000
                            });
                        }
                    },
                    error: function(e) {
                    }
                })
            })
        },
        editAddr: function() {
            $(document).on('click', '[data-role="addredit"]', function() {
                var addrId = $(this).attr('data-addrId');
                $(".member-address-submit").addClass("address-add");
                $('.action-add-address').addClass('isDisabled');
                $('[data-role="submit"]').addClass('isEdit').removeClass('isAdd');
                $('.cover').show();

                var $this = $(this);
                var name = $this.attr('data-name');
                var region = $this.attr('data-region');
                var addr = $this.attr('data-addr');
                var zip = $this.attr('data-zip');
                var tel = $this.attr('data-tel');
                var mobile = $this.attr('data-mobile');
                $('[data-role="submit"]').attr('data-addrId', $this.attr('data-addrId'));

                $('[data-role="region"]').val(region);
                $('[data-role="addr"]').val(addr);
                if(zip != 'null') $('[data-role="zip"]').val(zip);
                $('[data-role="name"]').val(name);
                if(mobile != 'null') $('[data-role="mobile"]').val(mobile);
                if(tel != 'null') $('[data-role="tel"]').val(tel);

            })
        },
        cancelHandle: function() {
            $(document).on('click', '[data-role="cancel"]', function() {
                $(".member-address-submit").removeClass("address-add");
                $('.action-add-address').removeClass('isDisabled');
                $('[data-role="submit"]').removeClass('isAdd').removeClass('isEdit');
                $('.cover').hide();
                $('[data-role="submit"]').removeAttr('data-addrId');
            })
        },
        submitHandle: function() {
            $(document).on('click', '[data-role="submit"]', function() {
                var region = $('[data-role="region"]').val();
                var addr = $('[data-role="addr"]').val().trim();
                var zip = $('[data-role="zip"]').val().trim();
                var name = $('[data-role="name"]').val().trim();
                var mobile = $('[data-role="mobile"]').val().trim();
                var tel = $('[data-role="tel"]').val().trim();
                var reg = /^((1[3456789])|(9[28]))\d{9}$/;
                // var isDefault = $('#for_set_default').prop('checked');

                if(region == -1 || addr == '' || name == '' || mobile == '') {
                    iosOverlay({
                        text: '请完善地址信息！',
                        duration: 1000
                    });
                    return;
                }
                if(!reg.test(mobile)) {
                    iosOverlay({
                		text: "手机号码格式不正确!",
                        duration: 1000
                	});
                    return;
                }
                var $this = $(this);
                if($this.hasClass('isAdd')) {
                    $.ajax({
                        url: wsCommon.host + '/api/memberAddrs/addMemberAddrs',
                        dataType: wsCommon.jsonType,
                        data: {
                            name: name,
                            mobile: mobile,
                            tel: tel,
                            regionId: region,
                            addr: addr,
                            zip: zip
                        },
                        success: function(res) {
                            if(res.code == 1) {
                                if(!res.result) {
                                    wsCommon.setCookie('go_back_link', window.location.href);
                                    window.location.href = './passport-login.html';
                                    return;
                                }
                                iosOverlay({
                                    text: '添加成功',
                                    duration: 1000,
                                    onbeforehide: function() {
                                        window.location.reload()
                                    }
                                });
                            } else {
                                iosOverlay({
                                    text: res.reason,
                                    duration: 1000
                                });
                            }
                        },
                        error: function(e) {
                        }
                    })
                } else if($this.hasClass('isEdit')) {
                    var addrId = $this.attr('data-addrId');
                    $.ajax({
                        url: wsCommon.host + '/api/memberAddrs/editMemberAddrs',
                        dataType: wsCommon.jsonType,
                        data: {
                            addrId: addrId,
                            name: name,
                            mobile: mobile,
                            tel: tel,
                            regionId: region,
                            addr: addr,
                            zip: zip
                        },
                        success: function(res) {
                            if(res.code == 1) {
                                if(!res.result) {
                                    wsCommon.setCookie('go_back_link', window.location.href);
                                    window.location.href = './passport-login.html';
                                    return;
                                }
                                iosOverlay({
                                    text: '编辑成功',
                                    duration: 1000,
                                    onbeforehide: function() {
                                        window.location.reload()
                                    }
                                });
                            } else {
                                iosOverlay({
                                    text: res.reason,
                                    duration: 1000
                                });
                            }
                        },
                        error: function(e) {
                        }
                    })
                }


            })
        },
        init: function() {
            this.getList();
            this.setDefault();
            this.delAddr();
            this.editAddr();
            this.cancelHandle();
            this.submitHandle();

            /*********编辑地址*********/
    		$(".action-add-address").click(function(){
                if($(this).hasClass('isDisabled')) return;
                if($('[data-role="resultnum"]').html() >= 10) {
                    iosOverlay({
                        text: '最多可添加10个收货地址',
                        duration: 1000
                    });
                    return;
                }
                $('[data-role="submit"]').addClass('isAdd');
    			$(".member-address-submit").toggleClass("address-add");

    		})
        }
    }

    receiver.init();
})
